package xcmg.device.web.split;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.mapper.CompanySupplierMapper;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.split.RestoreService;
import xcmg.device.service.vo.split.RestorePdaVO;
import xcmg.device.service.vo.split.RestoreVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/restore")
public class RestoreController extends BasicController {

    @Autowired
    private RestoreService restoreService;

    @Autowired
    private CompanySupplierMapper companySupplierMapper;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<RestoreVO> condition)throws Exception{
        RestoreVO restoreVO = condition.getFilters();
        restoreVO.setOrgId(getOrgId());
        condition.setFilters(restoreVO);
        PageCommonVO pageCommonVO = restoreService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(restoreService.item(id));
    }

    @RequestMapping(value = "/restorePda",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult restorePda(@RequestBody RestorePdaVO restorePdaVO)throws Exception{
        try {
            return actionResult(restoreService.restorePda(restorePdaVO, getUserId(), getOrgId()));
        }catch (Exception e){
            getExceptionAllinformation(e);
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/getRestoreAmount",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult getRestoreAmount(@RequestBody RestorePdaVO restorePdaVO)throws Exception{
        return actionValueResult(restoreService.getRestoreAmount(restorePdaVO.getRestoreOrder(), getOrgId()));
    }

    @RequestMapping(value = "/listByStatus",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult listByStatus(@RequestBody RestorePdaVO restorePdaVO)throws Exception{
        return actionValueResult(restoreService.listByStatus(restorePdaVO.getRestoreOrder(), restorePdaVO.getType(), getOrgId()));
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        restoreService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(RestoreVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        restoreService.exportAll(condition);
    }

    private String getExceptionAllinformation(Exception ex){
        String sOut = "\r\n";
        sOut += ex.getMessage();
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            sOut += "\tat " + s + "\r\n";
        }
        return sOut;
    }
}
